package entity_user

import (
	"chat_gin/entity/dto"
	"chat_gin/entity/models"
	"chat_gin/utils"
	"fmt"
	"time"
)

// LoginOpe 登录
func LoginOpe(param dto.LoginInfo) bool {
	var UserModel models.UserBasic
	err := utils.DB.Model(&UserModel).Where("name = ?", param.Name).Where("password =? ", param.Pwd).First(&UserModel).Error
	if err != nil {
		fmt.Println(err)
		return false
	} else {
		if UserModel.ID > 0 {
			return true
		} else {
			return false
		}
	}
}

// RegOpe 注册接口
func RegOpe(param dto.RegInfo) bool {
	var UserModel models.UserBasic
	var getNum int64
	utils.DB.Model(&UserModel).Where("name = ?", param.Name).Where("password =? ", param.Pwd).Count(&getNum).First(&UserModel)
	fmt.Println("注册查询到的数据", UserModel)
	if getNum > 0 {
		return false
	} else {
		// 校验通过,开始注册用户
		fmt.Println("校验通过,开始注册用户=", UserModel.ID)
		newUser := models.UserBasic{Name: param.Name, Password: param.Pwd, LoginTime: time.Now(),
			HeartbeatTime: time.Now(),
			LogOutTime:    time.Now()}
		err := utils.DB.Model(&UserModel).Create(&newUser).Error
		fmt.Println("注册结果", newUser.ID)
		if err != nil {
			fmt.Println(err)
			return false
		} else {
			return true
		}
	}
}
